<script lang="ts" setup name="StockPlan">
import { CirclePlus, Delete, EditPen } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { ProTableInstance } from '~/library/components/ProTable/interface'
import { Procure } from '~/src/api/interface/procure'
import { checkStockPlanAPI, getStockPlanAPI } from '~/src/api/procure'
import { stockPlanColumn } from './info/columns'

const router = useRouter()
const route = useRoute()

const proTableRef = ref<ProTableInstance>()
const initParam = ref({ orderType: 1 })

const toCreate = (data?: Procure.StockPlan) => {
  // 跳转详情页面
  if (data) {
    console.log(data, data.id)
    router.push({
      path: '/procure/stockPlanEdit',
      query: {
        id: data.orderId,
        type: data.status === 4 ? 'audited' : 'edit',
      },
    })
  } else {
    router.push({
      path: '/procure/stockPlanEdit',
      query: {
        id: 0,
        type: 'add',
      },
    })
  }
}

// 审核盘点计划
const toCheck = async (row: any) => {
  await useHandleData(checkStockPlanAPI, { id: row.orderId }, `完成【${row.orderNo}】盘点计划`)
  proTableRef.value?.getTableList()
}

// 删除计划（盘点中不能删除）
const toDelete = async () => {
  await useDeleteData(checkStockPlanAPI, proTableRef.value as ProTableInstance)
  // proTableRef.value?.getTableList()
}
</script>

<template>
  <div class="auto-height-container">
    <pro-table
      ref="proTableRef"
      :columns="stockPlanColumn"
      highlight-current-row
      :indent="20"
      :init-param="initParam"
      :isCard="false"
      :pagination="true"
      :request-api="getStockPlanAPI"
      row-key="id"
      title="盘点计划列表"
    >
      <template #tableHeader>
        <!-- <el-button v-auth="'add'" type="primary" :icon="CirclePlus" @click="toCreate(0)"> 新增 </el-button>  -->
        <el-button v-auth="'add'" :icon="CirclePlus" type="primary" @click="toCreate()">新增盘点计划</el-button>
        <el-button v-auth="'delete'" :icon="Delete" @click="toDelete">删除</el-button>
      </template>
      <template #orderNo="{ row }">
        <el-button :icon="EditPen" link type="primary" @click.stop="toCreate(row)">
          {{ row.orderNo }}
        </el-button>
      </template>
      <template #operation="{ row }">
        <el-button v-if="row.status === 2" v-auth="'check'" link type="primary" @click="toCheck(row)">完成盘点</el-button>
        <!-- <el-button v-if="row.status === 4" link type="primary" @click="toCreate(row.orderId)">查看</el-button> -->
        <!-- <el-button v-if="row.status !== 4" v-auth="'edit'" link type="primary" @click="toCreate(row.orderId)">编辑</el-button>
        <el-button v-if="row.status === 4" link type="primary" @click="toCreate(row.orderId)">查看</el-button> -->
        <!-- <el-button v-if="row.status === 1" v-auth="'delete'" link type="primary">删除</el-button> -->
      </template>
    </pro-table>
  </div>
</template>

<style lang="scss" scoped></style>
